package com.app.shop.service.system.impl;

import com.app.shop.util.SysModel;
import com.app.shop.dao.system.SysAuthMapper;
import com.app.shop.entity.SysAuth;
import com.app.shop.entity.SysAuthExample;
import com.app.shop.entity.query.SysAuthQuery;
import com.app.shop.service.system.SysAuthService;
import com.app.shop.entity.show.SysAuthShow;
import com.app.shop.util.ClassUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 权限服务
 *
 * @author chenliwei
 * @version v1.0 2019-3-11
 */
@Service("sysAuthService")
public class SysAuthServiceImp implements SysAuthService {

    private static final Logger log = LoggerFactory.getLogger(SysAuthServiceImp.class);

    @Autowired
    private SysAuthMapper sysAuthMapper;

    @Override
    public List<SysAuth> getSysAuthListAll() {
        //查询条件
        SysAuthExample sysAuthExample = new SysAuthExample();
        SysAuthExample.Criteria criteria = sysAuthExample.createCriteria();
        criteria.andStatusEqualTo(1);
        return sysAuthMapper.selectByExample(sysAuthExample);

    }

    @Override
    public Page<SysAuth> getSysAuthList(SysAuthQuery sysAuthQuery) {
        //查询条件
        SysAuthExample sysAuthExample = new SysAuthExample();
        SysAuthExample.Criteria criteria = sysAuthExample.createCriteria();
        //criteria.andStatusEqualTo(1);
        //权限名
        if (sysAuthQuery.getAuthName() != null && sysAuthQuery.getAuthName().trim().length() > 0) {
            criteria.andAuthNameLike('%' + sysAuthQuery.getAuthName() + '%');
        }
        //权限状态
        if (sysAuthQuery.getStatus() != null) {
            criteria.andStatusEqualTo(sysAuthQuery.getStatus());
        }
        //模拟分页查询
        PageHelper.startPage(sysAuthQuery.getCurrentPage(), sysAuthQuery.getPageSize());
        return sysAuthMapper.selectByExample(sysAuthExample);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateAuthStart(SysAuthQuery sysAuthQuery) {
        SysAuth sysAuth = new SysAuth();
        sysAuth.setStatus(1);
        sysAuth.setVersion(new Date());
        sysAuth.setAuthId(sysAuthQuery.getAuthId());
        sysAuth.setUpdateUserId(sysAuthQuery.getOptionUserId());
        sysAuthMapper.updateByPrimaryKeySelective(sysAuth);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateAuthStop(SysAuthQuery sysAuthQuery) {
        //超级管理员权限不能被重新启用或者禁用
        if (sysAuthQuery.getAuthId() != 1001) {
            SysAuth sysAuth = new SysAuth();
            sysAuth.setStatus(-1);
            sysAuth.setVersion(new Date());
            sysAuth.setAuthId(sysAuthQuery.getAuthId());
            sysAuth.setUpdateUserId(sysAuthQuery.getOptionUserId());
            sysAuthMapper.updateByPrimaryKeySelective(sysAuth);
        }
    }

    @Override
    public SysAuthShow getAuthInfo(SysAuthQuery sysAuthQuery) {
        SysAuth sysAuth = sysAuthMapper.selectByPrimaryKey(sysAuthQuery.getAuthId());
        SysAuthShow sysAuthShow = new SysAuthShow();
        try {
            ClassUtil.fatherToChild(sysAuth, sysAuthShow);
            //码表值补全
            sysAuthShow.dataSet();
        } catch (Exception e) {
            log.error("权限信息补全码表过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysAuthShow;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel addAuthInfo(SysAuth sysAuth) {
        SysModel sysModel = new SysModel();
        try {
            //查询权限是否存在
            SysAuthExample sysAuthExample = new SysAuthExample();
            SysAuthExample.Criteria criteria = sysAuthExample.createCriteria();
            criteria.andAuthNameEqualTo(sysAuth.getAuthName());
            sysAuth.setCreateDate(new Date());
            sysAuth.setVersion(new Date());

            Long cnt = sysAuthMapper.countByExample(sysAuthExample);
            if (cnt < 1) {
                sysAuthMapper.insert(sysAuth);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("添加失败，权限已经存在！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("添加失败，添加权限过程发生异常请重试！");
            log.error("权限添加过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel updateAuthInfo(SysAuth sysAuth) {
        SysModel sysModel = new SysModel();
        if (sysAuth.getAuthId() != 1001) {
            try {
                // 修改要和之前的版本时间对应，不然不允许修改
                SysAuthExample sysAuthExample = new SysAuthExample();
                SysAuthExample.Criteria criteria = sysAuthExample.createCriteria();
                criteria.andAuthIdEqualTo(sysAuth.getAuthId()).andVersionEqualTo(sysAuth.getVersion());
                List<SysAuth> list = sysAuthMapper.selectByExample(sysAuthExample);
                if (list != null&& list.size()>0) {
                    SysAuth  sysAuthUp=list.get(0);
                    sysAuthUp.setStatus(sysAuth.getStatus());
                    sysAuthUp.setAuthName(sysAuth.getAuthName());
                    sysAuthUp.setRemark(sysAuth.getRemark());
                    sysAuthUp.setVersion(new Date());
                    sysAuthUp.setUpdateUserId(sysAuth.getUpdateUserId());
                    sysAuthMapper.updateByExample(sysAuthUp, sysAuthExample);
                    sysModel.setSuccess(true);
                } else {
                    sysModel.setSuccess(false);
                    sysModel.setMsg("修改失败，当前信息已经被他人修改请重试！");
                }
            } catch (Exception e) {
                sysModel.setSuccess(false);
                sysModel.setMsg("修改失败，修改过程发生异常请重试！");
                log.error("权限修改过程中发生异常:" + e);
                e.printStackTrace();
            }
        } else {
            sysModel.setSuccess(false);
            sysModel.setMsg("修改失败，超级管理员不允许被修改！");
        }
        return sysModel;

    }
}
